from sqlalchemy.orm import Session
from schema import Lessee_schema
from model import Lessee_model



def get_Lessee(db: Session, ID: int):
    return db.query(Lessee_model.Lessee).filter(Lessee_model.Lessee.ID == ID).first()


def get_all_Lessee(db: Session):
    return db.query(Lessee_model.Lessee).all()


def delete_Lessee(db: Session, ID):
    to_delete = db.query(Lessee_model.Lessee).filter(Lessee_model.Lessee.ID == ID)
    deleted = to_delete.delete()
    if deleted == 0:
        return None
    else:
        db.commit()
        return True


def create_Lessee(db: Session, Lessee: Lessee_schema.Lessee):
    db_Lessee = Lessee_model.Lessee(
        Account=Lessee.Account,
        Name=Lessee.Name,
        Password=Lessee.Password,
        Phone=Lessee.Phone,
        Email=Lessee.Email,
    )
    db.add(db_Lessee)
    db.commit()
    db.refresh(db_Lessee)
    return db_Lessee


def update_Lessee(db: Session, ID, field_name, new_value):
    db_Lessee = db.query(Lessee_model.Lessee).filter(Lessee_model.Lessee.ID == ID).first()
    setattr(db_Lessee, field_name, new_value)
    db.commit()
    db.refresh(db_Lessee)
    return db_Lessee
